from openerp.osv import fields, osv

class res_partner(osv.osv):
    _name = 'res.partner'
    _inherit = 'res.partner'
    def copy(self, cr, uid, record_id, default=None, context=None):
        if default is None:
            default = {}
        default.update({'delivery': []})
        super(res_partner, self).copy(cr, uid, record_id, default, context)
    
    def _cal_point (self, cr, uid, ids, field_name, arg, context=None):
        res = dict([(id, {'point': 10}) for id in ids])
        move_pool=self.pool.get('stock.picking.out')
        for id in ids:
            rids = move_pool.search(cr, uid, [
            ('partner_id', '=', id),
            ('type', '=', 'out'),
            ('state','in',('confirmed','assigned','pending','done'))
            ], order='value desc')
            if len(rids) > 0:
                sum = 0
                for rid in rids:
                    sum = move_pool.browse(cr, uid, rid, context=context).point + sum
                res[id]['point'] = sum / len(rids)    
        return res
    
    
    
    _columns = {
        'delivery': fields.one2many('stock.move', 'partner_id', 'Products', domain=[('state','in',('confirmed','assigned','pending','done')),('picking_id.type', '=', 'out'),('location_dest_id.scrap_location', '=', False)]),
        'user_name': fields.char('Username', size=24),
        'password': fields.char('Password', size=24),
        'point': fields.function(_cal_point, string="Point", type='integer', multi='pickings', readonly=True)
    }
    _defaults= {
        'password': 'dlv1234',
                }
    _sql_constraints = [('user_name_uniq','unique(user_name)', 'Username must be unique!'),
                        ('point0','CHECK(point > 0)',  ('Please set point between 0 and 10.')),
                        ('point10','CHECK(point < 53)', ('Please set a week between 0 and 10.')),
                        ]
    
    def reset_pass(self, cr, uid, id, context=None):
        return self.write(cr, uid, id, {'password': 'dlv1234'})
res_partner()


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

